// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var js_platform_main = require("./platform_main.js");
	var tools = require("tools");
	var constant = require("constant");
	var ii;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var left;
	var pickupId;
	var init = function(page) {
		left = page.params.left || base.getUrlParam("left");
		$(".left").removeClass("none");
		leftMenu.init(6);
		js_platform_main.init(0);
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#ffffff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		inquireDeliveryPageList();
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		$("#deliveryNotice_list .searchButon").unbind("click").click(function() {
			pageIndex = 1;
			if ($("#deliveryNotice_list .searchStatus").val() == 1) {
				inquireDeliveryPageList1();
			} else {
				inquireDeliveryPageList();
			}
		});
	};

	//查询放货确认-提货信息列表
	var inquireDeliveryPageList = function() {
		var productType = $("#deliveryNotice_list select[name=productType]").val();
		base.fnAjax({
			action: "inquireWaitDeliveryPickupApplicationPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				pickupId: $("#deliveries").val().split(','),
				financeNumber: $("#deliveryNotice_list .financeNumber").val(),
				waybillNumber: $("#deliveryNotice_list .waybillNumber").val(),
				enterpriseName: $("#deliveryNotice_list .enterpriseName").val(),
				bulkstockName: $("#deliveryNotice_list .bulkstockName").val(),
				productType: productType,
				caseNumber: $("#deliveryNotice_list .caseNumber").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#deliveryListTpl").html();
					var html = juicer(tpl, data);
					$("#deliveryListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#recievespagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireDeliveryPageList();
						});

					tools.changeWarningColor($("#deliveryListBody"), "deliveryNoticeCreateDatetime");
					
					//点击收货通知
					$("#deliveryListBody").on("click", ".details_link", function() {
						var pickupId;
						pickupId = $(this).parents("tr").attr("pickupId");
						var totalLoanAmount;
						totalLoanAmount = $(this).parents("tr").attr("totalLoanAmount");
						var financeNumber;
						financeNumber = $(this).parents("tr").attr("financeNumber");
						router.route("main/deliveryNotice.html?pickupId=" + pickupId + "&left=" + left, true);
					});
					
					$(".autoDelivery").unbind("click").click(function(){
						var $this = $(this);
						ii = layer.confirm('是否确认一键放货？', {
						  btn: ['确定','取消'] //按钮
						}, function(){
							autoDeliveryInventory($this.parents("tr").attr("pickupId"));
						}, function(){
						  
						});
					});
					
					//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + $(this).parents("tr").attr("financeId"), true);
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//查询放货确认-提货信息列表
	var inquireDeliveryPageList1 = function() {
		var productType = $("#deliveryNotice_list select[name=productType]").val();
		base.fnAjax({
			action: "inquireWaitDeliveryPickupApplicationPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				financeNumber: $("#deliveryNotice_list .financeNumber").val(),
				waybillNumber: $("#deliveryNotice_list .waybillNumber").val(),
				enterpriseName: $("#deliveryNotice_list .enterpriseName").val(),
				bulkstockName: $("#deliveryNotice_list .bulkstockName").val(),
				productType: productType,
				caseNumber: $("#deliveryNotice_list .caseNumber").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#deliveryListTpl").html();
					var html = juicer(tpl, data);
					$("#deliveryListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#recievespagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireDeliveryPageList1();
						});

					$.each($(".applicationDateTime4"), function() {
						var msec = tools.timeCalculator(new Date($(this).attr("applicationDateTime").replace(/-/g, "/")), new Date());
						var t = msec / 3600000;
						if (t >= 3 && t < 6) {
							$(this).find("td").css({
								"background-color": "#fcfae0",
								"color": "#000"
							});
						} else if (t > 6) {
							$(this).find("td").css({
								"background-color": "#ffeeee",
								"color": "#000"
							});
						}
					});
					//点击收货通知
					$("#deliveryListBody").on("click", ".details_link", function() {
						var pickupId;
						pickupId = $(this).parents("tr").attr("pickupId");
						var totalLoanAmount;
						totalLoanAmount = $(this).parents("tr").attr("totalLoanAmount");
						var financeNumber;
						financeNumber = $(this).parents("tr").attr("financeNumber");
						router.route("main/deliveryNotice.html?pickupId=" + pickupId + "&left=" + left, true);
					});
					//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeIds, true);
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//一键放货
	var autoDeliveryInventory = function(pickupId) {
		base.fnAjax({
			action: "autoDeliveryInventory.json",
			oData: {
				pickupId: pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "放货成功！");
					router.route("main/deliveryNotice_list.html?left=8", true);
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});